package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.ReportMapper;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private ReportMapper reportMapper;
    /**
     * 获取会员统计数据
     * @return
     */
    public Map<String, Object> getMemberReport() {
        Map<String,Object> map =new HashMap<>();


        //获取年月的数组
        List<String> months = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.add(Calendar.MONTH,-12);//当前时间往前推一个月
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//循环到的日期往后推一个月
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date)+"."+calendar.getActualMaximum(calendar.DAY_OF_MONTH));
        }
        map.put("months",months);
        List<Integer> list =new ArrayList<>();
        months.forEach(month->{
            int memberCount = 0;
            try {
                memberCount = reportMapper.getMembers(DateUtils.parseString2Date(month,"yyyy.MM.dd"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            list.add(memberCount);
        });
        map.put("memberCount",list);
        return map;
    }

    /**
     * 获取套餐数据
     * @return
     */
    public Map<String, Object> getSetmealReport() {
        //前端需要setmealCount，setmealNames两个值，声明一个Map对象动态存储
        Map<String,Object> map = new HashMap<>();
        //从数据库中获取setmealCount（name,count）
        List<Map<String ,Object>> setmealCount = reportMapper.getSetMeal();
        map.put("setmealCount",setmealCount);
        //setmealCount中的name值和setmealNames里的值相同
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map1 : setmealCount) {
            String name = (String) map1.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames",setmealNames);
        return map;
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //获取今天的日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一日期
        String firstDay4ThisWeek = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //日期：
        map.put("reportDate",today);
        //今日新增会员数
        Integer todayNewMember = reportMapper.findMemberCountByDate(today);
        map.put("todayNewMember",todayNewMember);
        //总会员数
        Integer totalMember = reportMapper.findMemberTotalCount();
        map.put("totalMember",totalMember);
        //本周新增会员数  thisWeekNewMember
        Integer thisWeekNewMember =reportMapper.findMemberCount(firstDay4ThisWeek);
        map.put("thisWeekNewMember",thisWeekNewMember);
        //本月新增会员数   thisMonthNewMember
        Integer thisMonthNewMember =reportMapper.findMemberCount(firstDay4ThisMonth);
        map.put("thisMonthNewMember",thisMonthNewMember);
        //今日预约数  todayOrderNumber
        Integer todayOrderNumber = reportMapper.findOrderCountByDate(today);
        map.put("todayOrderNumber",todayOrderNumber);
        //今日到诊数  todayVisitsNumber
        Integer todayVisitsNumber = reportMapper.findVisitCountByDate(today);
        map.put("todayVisitsNumber",todayVisitsNumber);
        //本周预约数  thisWeekOrderNumber
        Integer thisWeekOrderNumber = reportMapper.findOrderCount(firstDay4ThisWeek);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //本周到诊数  thisWeekVisitsNumber
        Integer thisWeekVisitsNumber = reportMapper.findVisitCount(firstDay4ThisWeek);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //本月预约数  thisMonthOrderNumber
        Integer thisMonthOrderNumber = reportMapper.findOrderCount(firstDay4ThisMonth);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        //本月到诊数  thisMonthVisitsNumber
        Integer thisMonthVisitsNumber = reportMapper.findVisitCount(firstDay4ThisMonth);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        //热门套餐
        List<Map> hotSetmeal = reportMapper.findHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }
}
